package com.jm.web.builder;


import javax.servlet.jsp.JspWriter;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jm.web.form.support.FormItem;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * Sep 17, 2011 5:00:40 PM
 */
public abstract class BaseBuilder {
	Log log = LogFactory.getLog(BaseBuilder.class);
	
	protected BaseTag baseTag = null ;
//	protected JspWriter writer = null ;
	protected JspWriter out = null;

	public void init(BaseTag tag){
		this.baseTag = tag ;
	}
    
	public void RenderStart(){
		
	}
	
	public void RenderBody(){
		
	}
	
    /**
	 * 输出对象界面
	 * @param writer
	 */
	public void RenderEnd(){
		OutputTagBegin();
		OutputTagBody();
		OutputTagEnd();
	}
	
	/**
     * 输出TAG头
     * @param writer
     * @throws Exception
     */
    protected void OutputTagBegin()
    {
    	
    }
    
    /**
     * 输出TAG Body
     * @param writer
     * @throws Exception
     */
    protected void OutputTagBody()
    {
    	
    }
    
    /**
     * 输出TAG尾部
     * @param writer
     * @throws Exception
     */
    protected void OutputTagEnd()
    {
    	
    }

	public BaseTag getBaseTag() {
		return this.baseTag ;
	}
	
	protected void prepareParams(FormItem item){
		if( this.getBaseTag() != null && this.getBaseTag().getExtAttrList() != null)
		{
			for(String key : this.getBaseTag().getExtAttrList().keySet())
			{
				Object object = this.getBaseTag().getExtAttrList().get(key);
				if(object instanceof String){
					item.OnSetProperty(key, (String)object);
				}else if(object instanceof Integer){
					item.OnSetProperty(key, (String)object);
				}else if(object instanceof Double){
					item.OnSetProperty(key, (String)object);
				}else if(object instanceof Float){
					item.OnSetProperty(key, (String)object);
				}else{
					item.OnSetProperty(key, object);
				}
			}
		}
	}

	public JspWriter getOut() {
		return out;
	}

	public void setOut(JspWriter out) {
		this.out = out;
	}
}
